package edu.whu.demo.controler;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.whu.demo.mydata;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController()
@RequestMapping("goods")
public class goodscontroller {
@PutMapping("/1")
public void add(){
    System.out.println("增加商品成功");
}
    @GetMapping("/2")
    public void decline(){
        System.out.println("减少商品成功");
    }
    @GetMapping("/3")
    public void change(){
        System.out.println("改变商品成功");
    }
    @GetMapping("/4")
    public mydata serach1(){
        System.out.println("查询到您需要的商品");
        mydata t=new mydata();
        t.id=2;
        t.name="羊肉";
        t.price=80;
        return t;
    }
    @GetMapping("")
    public mydata manage(@RequestBody String json){
        System.out.println("开始处理");
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            mydata product = objectMapper.readValue(json, mydata.class);

            // 在这里可以对product进行处理，比如保存到数据库等操作
          if (product.id==1){
              add();
              System.out.println(product.price);
            }
         else   if (product.id==2){
                decline();
                System.out.println(product.price);
            }
         else   if (product.id==3){
                change();
                System.out.println(product.price);
            }
        else    if (product.id==4){

                System.out.println(product.price);
                return serach1();
            }
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }
}
